{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "# 机器学习使用 这里才开始拷贝\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from scipy.stats.mstats import winsorize\n",
    "from sklearn.linear_model import LinearRegression, Ridge\n",
    "\n",
    "# numpy ,panda\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import logging as logger"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "stock = \"600526.XSHG\"\n",
    "days=20\n",
    "\n",
    "stock_price = np.array(\n",
    "[(4.44, 4.11,  4.45, 4.13) ,  (4.2 , 4.08,  4.26, 4.09),\n",
    " (4.06, 4.03,  4.3 , 4.21) ,  (4.18, 4.16,  4.49, 4.42),\n",
    " (4.41, 4.39,  4.86, 4.86) ,  (5.35, 5.35,  5.35, 5.35),\n",
    " (5.89, 5.89,  5.89, 5.89) ,  (6.3 , 6.02,  6.48, 6.48),\n",
    " (6.14, 6.14,  7.12, 6.87) ,  (6.76, 6.54,  7.56, 7.56),\n",
    " (7.86, 7.18,  8.32, 8.32) ,  (9.15, 8.75,  9.15, 9.15),\n",
    " (9.7 , 8.24, 10.  , 8.78) ,  (8.3 , 7.9 ,  8.64, 8.2 ),\n",
    " (8.1 , 7.71,  8.49, 8.25) ,  (7.79, 7.43,  7.96, 7.51),\n",
    " (7.27, 7.15,  7.84, 7.4 ) ,  (7.21, 7.01,  7.45, 7.3 ),\n",
    " (7.3 , 6.97,  7.52, 6.98) ,  (6.95, 6.9 ,  7.23, 7.11)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [],
   "source": [
    "# stock_price = history_bars(stock, days, '1d',[ 'open','low','high','close'])\n",
    "\n",
    "stock_price=pd.DataFrame(stock_price,columns=[ 'open','low','high','close'])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "data": {
      "text/plain": "    open   low   high  close\n0   4.44  4.11   4.45   4.13\n1   4.20  4.08   4.26   4.09\n2   4.06  4.03   4.30   4.21\n3   4.18  4.16   4.49   4.42\n4   4.41  4.39   4.86   4.86\n5   5.35  5.35   5.35   5.35\n6   5.89  5.89   5.89   5.89\n7   6.30  6.02   6.48   6.48\n8   6.14  6.14   7.12   6.87\n9   6.76  6.54   7.56   7.56\n10  7.86  7.18   8.32   8.32\n11  9.15  8.75   9.15   9.15\n12  9.70  8.24  10.00   8.78\n13  8.30  7.90   8.64   8.20\n14  8.10  7.71   8.49   8.25\n15  7.79  7.43   7.96   7.51\n16  7.27  7.15   7.84   7.40\n17  7.21  7.01   7.45   7.30\n18  7.30  6.97   7.52   6.98\n19  6.95  6.90   7.23   7.11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>open</th>\n      <th>low</th>\n      <th>high</th>\n      <th>close</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>4.44</td>\n      <td>4.11</td>\n      <td>4.45</td>\n      <td>4.13</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.20</td>\n      <td>4.08</td>\n      <td>4.26</td>\n      <td>4.09</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4.06</td>\n      <td>4.03</td>\n      <td>4.30</td>\n      <td>4.21</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4.18</td>\n      <td>4.16</td>\n      <td>4.49</td>\n      <td>4.42</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4.41</td>\n      <td>4.39</td>\n      <td>4.86</td>\n      <td>4.86</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>5.35</td>\n      <td>5.35</td>\n      <td>5.35</td>\n      <td>5.35</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>5.89</td>\n      <td>5.89</td>\n      <td>5.89</td>\n      <td>5.89</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>6.30</td>\n      <td>6.02</td>\n      <td>6.48</td>\n      <td>6.48</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>6.14</td>\n      <td>6.14</td>\n      <td>7.12</td>\n      <td>6.87</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>6.76</td>\n      <td>6.54</td>\n      <td>7.56</td>\n      <td>7.56</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>7.86</td>\n      <td>7.18</td>\n      <td>8.32</td>\n      <td>8.32</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>9.15</td>\n      <td>8.75</td>\n      <td>9.15</td>\n      <td>9.15</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>9.70</td>\n      <td>8.24</td>\n      <td>10.00</td>\n      <td>8.78</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>8.30</td>\n      <td>7.90</td>\n      <td>8.64</td>\n      <td>8.20</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>8.10</td>\n      <td>7.71</td>\n      <td>8.49</td>\n      <td>8.25</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>7.79</td>\n      <td>7.43</td>\n      <td>7.96</td>\n      <td>7.51</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>7.27</td>\n      <td>7.15</td>\n      <td>7.84</td>\n      <td>7.40</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>7.21</td>\n      <td>7.01</td>\n      <td>7.45</td>\n      <td>7.30</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>7.30</td>\n      <td>6.97</td>\n      <td>7.52</td>\n      <td>6.98</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>6.95</td>\n      <td>6.90</td>\n      <td>7.23</td>\n      <td>7.11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 67
    }
   ],
   "source": [
    "stock_price"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [],
   "source": [
    "# 找到最低点和最低点的index\n",
    "lowest=stock_price.loc[:,'low'].min()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [
    {
     "data": {
      "text/plain": "   open   low  high  close\n2  4.06  4.03   4.3   4.21",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>open</th>\n      <th>low</th>\n      <th>high</th>\n      <th>close</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>4.06</td>\n      <td>4.03</td>\n      <td>4.3</td>\n      <td>4.21</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 69
    }
   ],
   "source": [
    "stock_price[stock_price['low']==lowest]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [],
   "source": [
    "lowest_index = stock_price[stock_price['low']==lowest].index[0]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 71
    }
   ],
   "source": [
    "lowest_index"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "outputs": [],
   "source": [
    "logger.info(\"%s 天内最低点 %.2f\" % (days, lowest))\n",
    "logger.info(\"%s 天内最低点的index %d\" % (days, lowest_index))\n",
    " \n",
    "# 当天最低点出现\n",
    "if days == (lowest_index + 1):\n",
    "    logger.info(\"当天是最低点,取出收盘价作为上涨的价格\")\n",
    "    # 为啥不用最高价,因为最高价可以在最低价前面出现,不一定是反弹的\n",
    "    lowest_day_close = stock_price[:,3:4][lowest_index]\n",
    "    rocket_rate=(lowest_day_close/ lowest-1)\n",
    "\n",
    "    # return rocket_rate"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "outputs": [],
   "source": [
    " \n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [],
   "source": [
    "high_list=stock_price.iloc[lowest_index:,stock_price.columns.get_indexer(['high'])]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "outputs": [],
   "source": [
    "# 按列统计最大值\n",
    "highest=high_list.max(axis=0)[0]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "data": {
      "text/plain": "10.0"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 75
    }
   ],
   "source": [
    "\n",
    "highest "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% \n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "outputs": [],
   "source": [
    "# 这个方法求最高点\n",
    "highest_index=high_list.idxmax(axis=0)[0]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "outputs": [
    {
     "data": {
      "text/plain": "12"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 77
    }
   ],
   "source": [
    "highest_index"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% \n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "outputs": [],
   "source": [
    "rocket_rate=((highest / lowest)-1)\n",
    "# todo 如果上涨率在0.47到0.9之间 才进行下一步\n",
    "if 0.47<rocket_rate<0.9:\n",
    "    # 可以下一步\n",
    "    pass\n",
    "\n",
    "# return rocket_rate"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "outputs": [
    {
     "data": {
      "text/plain": "1.4813895781637716"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 79
    }
   ],
   "source": [
    "rocket_rate"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "outputs": [],
   "source": [
    "#   todo上涨天数不能超过7天\n",
    "rocket_days=(highest_index-lowest_index+1)\n",
    "if rocket_days<=7:\n",
    "    # 可以下一步\n",
    "    pass"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "data": {
      "text/plain": "11"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 81
    }
   ],
   "source": [
    "rocket_days\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "outputs": [
    {
     "data": {
      "text/plain": "20"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 82
    }
   ],
   "source": [
    "stock_price.shape[0]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [],
   "source": [
    "# 最高点到现在的天数,最高点作为第0天,最高点和当天是同一天,则天数为0,\n",
    "# 但是查询到的数据都是昨天以前的数据,所以最高点和当天不会重合\n",
    "highest_to_now_days=stock_price.shape[0]-highest_index"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "outputs": [],
   "source": [
    "stock_price['down_rate']=0\n",
    "stock_price['up_rate']=0"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "data": {
      "text/plain": "    open   low   high  close  down_rate  up_rate\n10  7.86  7.18   8.32   8.32          0        0\n11  9.15  8.75   9.15   9.15          0        0\n12  9.70  8.24  10.00   8.78          0        0\n13  8.30  7.90   8.64   8.20          0        0\n14  8.10  7.71   8.49   8.25          0        0\n15  7.79  7.43   7.96   7.51          0        0\n16  7.27  7.15   7.84   7.40          0        0\n17  7.21  7.01   7.45   7.30          0        0\n18  7.30  6.97   7.52   6.98          0        0\n19  6.95  6.90   7.23   7.11          0        0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>open</th>\n      <th>low</th>\n      <th>high</th>\n      <th>close</th>\n      <th>down_rate</th>\n      <th>up_rate</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>10</th>\n      <td>7.86</td>\n      <td>7.18</td>\n      <td>8.32</td>\n      <td>8.32</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>9.15</td>\n      <td>8.75</td>\n      <td>9.15</td>\n      <td>9.15</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>9.70</td>\n      <td>8.24</td>\n      <td>10.00</td>\n      <td>8.78</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>8.30</td>\n      <td>7.90</td>\n      <td>8.64</td>\n      <td>8.20</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>8.10</td>\n      <td>7.71</td>\n      <td>8.49</td>\n      <td>8.25</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>7.79</td>\n      <td>7.43</td>\n      <td>7.96</td>\n      <td>7.51</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>7.27</td>\n      <td>7.15</td>\n      <td>7.84</td>\n      <td>7.40</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>7.21</td>\n      <td>7.01</td>\n      <td>7.45</td>\n      <td>7.30</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>18</th>\n      <td>7.30</td>\n      <td>6.97</td>\n      <td>7.52</td>\n      <td>6.98</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>19</th>\n      <td>6.95</td>\n      <td>6.90</td>\n      <td>7.23</td>\n      <td>7.11</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 85
    }
   ],
   "source": [
    "stock_price.tail(10)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [],
   "source": [
    "if highest_to_now_days<3:\n",
    "    # 下跌不能超过3天\n",
    "    pass\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [],
   "source": [
    "for i in range(highest_index,stock_price.shape[0]):\n",
    "    # 跌幅\n",
    "    if i==highest_index:\n",
    "        stock_price.iloc[i,stock_price.columns.get_indexer(['down_rate'])] =1-(stock_price['close'][i]/highest)\n",
    "    else:\n",
    "        # 跌幅应该是最低点相对于最高点的跌幅\n",
    "        down_lowest=stock_price.iloc[highest_index:i+1,stock_price.columns.get_indexer(['low'])].min(0)[0]\n",
    "        stock_price.iloc[i,stock_price.columns.get_indexer(['down_rate'])] =1-(down_lowest/highest)\n",
    "    \n",
    "    # 反弹幅度 \n",
    "    if i==highest_index:\n",
    "        stock_price.iloc[i,stock_price.columns.get_indexer(['up_rate'])]=(stock_price['close'][i]/stock_price['low'][i])-1\n",
    "    else:\n",
    "        # 这个反弹幅度计算为下面两个取最大值:\n",
    "        # 1当天high/(前几天lowest和今天open的最小值)-1        \n",
    "        # 2 当天close/low-1\n",
    "        # todo 3如果当天收阳线,而且close/open-1>0.01,这时候,我们计算反弹幅度的时候,当天high/(前几天lowest和今天lowest的最小值)-1\n",
    "        \n",
    "        #  1当天high/(前几天lowest和今天open的最小值)-1\n",
    "        down_last_lowest=stock_price['low'][highest_index:i].min()  \n",
    "        downest0=min(down_last_lowest,stock_price['open'][i])  \n",
    "        highest0=stock_price['high'][i] \n",
    "        up_rate0=highest0/downest0-1\n",
    "        \n",
    "        # 2 当天(close/low)   -1\n",
    "        up_rate1=stock_price['close'][i]/stock_price['low'][i]-1\n",
    "        up_rate=max(up_rate0,up_rate1)\n",
    "    \n",
    "        stock_price.iloc[i,stock_price.columns.get_indexer(['up_rate'])]=up_rate\n",
    "    pass\n",
    "\n",
    " "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [],
   "source": [
    "stock_price['operate']=False\n",
    "# 反弹幅度\n",
    "# 计算每天的反弹幅度,如果反弹幅度超过11%,就移除,说明反弹过了\n",
    "for i in range(highest_index,stock_price.shape[0]):\n",
    "    buy_point=stock_price['down_rate'][i]>0.19\n",
    "    # 跌幅超过19%就可以买入\n",
    "    if buy_point:\n",
    "        # 方法采用统计前面几天的反弹幅度,如果有11%就结束了\n",
    "        # 不包括当天,因为当天反弹的需要分析  \n",
    "        already_up=stock_price.iloc[highest_index:i,:].query('up_rate>0.11').shape[0]>=1\n",
    "        if already_up:\n",
    "            # 前面几天反弹过了\n",
    "            stock_price.iloc[i,stock_price.columns.get_indexer(['operate'])]=False         \n",
    "            break\n",
    "            pass\n",
    "        else:\n",
    "            #之前几天没有反弹过\n",
    "            stock_price.iloc[i,stock_price.columns.get_indexer(['operate'])]=True \n",
    "        pass\n",
    "    else:\n",
    "        stock_price.iloc[i,stock_price.columns.get_indexer(['operate'])]=False\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "outputs": [],
   "source": [
    "# 一些计算太多小数,不需要那么多,保留三位即可\n",
    "stock_price=stock_price.round(decimals=3)\n",
    " "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "outputs": [],
   "source": [
    "stock_price['down_days']=0\n",
    "stock_price['up_days']=0\n",
    "# 这个是全局的,把每个股票的数据都进行汇总,拼接成新的数据,后面好做分析 \n",
    "analy_data=pd.DataFrame(data=None,columns=['open', 'low', 'high', 'close', 'down_rate', 'up_rate', 'operate', 'down_days', 'up_days', 'stock', 'date'])\n",
    "\n",
    " "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "outputs": [
    {
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-91-7504ecabb6b1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m# 记下up_rate>0.11的第一天\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m# 上涨的索引\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mup_index\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstock_price\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mhighest_index\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mstock_price\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mquery\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'up_rate>0.11'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;31m# 下跌的索引\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   2082\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2083\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mis_scalar\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2084\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mgetitem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2085\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2086\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mslice\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mIndexError\u001b[0m: index 0 is out of bounds for axis 0 with size 0"
     ],
     "ename": "IndexError",
     "evalue": "index 0 is out of bounds for axis 0 with size 0",
     "output_type": "error"
    }
   ],
   "source": [
    "# 把反弹到11%的第一天拿出来,记录下跌的天数,下跌幅度,反弹天数,反弹幅度,股票代码,日期等,后面做统计分析使用\n",
    "# 记下up_rate>0.11的第一天\n",
    "# 上涨的索引\n",
    "up_index = stock_price.iloc[highest_index:stock_price.shape[0],:].query('up_rate>0.11').index[0]\n",
    "\n",
    "# 下跌的索引\n",
    "down_index=stock_price.iloc[highest_index:up_index+1,stock_price.columns.get_indexer(['low'])].idxmin(axis=0)[0]\n",
    "\n",
    "# 下跌天数\n",
    "down_days=down_index-highest_index+1\n",
    "\n",
    "# 反弹天数\n",
    "up_days=up_index-down_index+1\n",
    "\n",
    "# 拿出这个up_index的数据,这个才是最终需要看的数据\n",
    "\n",
    "stock_key_data=stock_price.iloc[up_index,:]\n",
    "stock_key_data['stock']=stock\n",
    "stock_key_data['down_days']=down_days\n",
    "stock_key_data['up_days']=up_days\n",
    "stock_key_data['date']='not know'\n",
    "\n",
    " \n",
    "\n",
    "analy_data=analy_data.append(stock_key_data)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "analy_data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# \n",
    "# analy_data 需要进行'down_rate', 'up_rate'的*100操作,好分析一些,\n",
    "# 需要统计所有股票的,每天的情况,需要怎么写好呢\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "stock_price.tail(10) \n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
